package com.atguigu.statistics.service.impl;

import com.atguigu.statistics.entity.Daily;
import com.atguigu.statistics.feign.UcenterFeignServiceByStatistics;
import com.atguigu.statistics.mapper.DailyMapper;
import com.atguigu.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2021-12-24
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {
    @Autowired
    UcenterFeignServiceByStatistics ucenterFeignServiceByStatistics;

    @Override
    public void createStatisticsByDay(String createDay) {
        //删除已存在的统计对象
        QueryWrapper<Daily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculated", createDay);
        baseMapper.delete(dayQueryWrapper);


        //获取统计信息
        Integer registerNum = (Integer) ucenterFeignServiceByStatistics.registerCount(createDay).getData().get("countRegister");
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //创建统计对象
        Daily daily = new Daily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(createDay);

        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated",begin,end);
        queryWrapper.select("date_calculated",type);
        List<Daily> dailies = baseMapper.selectList(queryWrapper);

        List<String> dateCalculatedList = dailies.stream().map(Daily::getDateCalculated).collect(Collectors.toList());
        List<Integer> numDataList = dailies.stream().map(item -> {
            switch (type) {
                case "login_num":
                    return item.getLoginNum();
                case "register_num":
                    return item.getRegisterNum();
                case "video_view_num":
                    return item.getVideoViewNum();
                case "course_num":
                    return item.getCourseNum();
                default:
                    return null;
            }
        }).collect(Collectors.toList());

        Map<String, Object> map=new HashMap<>();
        map.put("dataList", numDataList);
        map.put("dateList", dateCalculatedList);
        return map;
    }
}
